package com.zxl.controller;

import com.zxl.constants.RedisConstants;
import com.zxl.feign.AddressFeign;
import com.zxl.feign.CarFeign;
import com.zxl.service.IOrderService;
import com.zxl.vo.Address;
import com.zxl.vo.Car;
import com.zxl.vo.Customer;
import com.zxl.vo.OrderParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private CarFeign carFeign;
    @Autowired
    private AddressFeign addressFeign;
    @Autowired
    private IOrderService orderService;

    @RequestMapping("/{page}.html")
    public String page(@PathVariable("page") String page,
                       String[] gids,
                       HttpSession session,
                       Model model) {

        Customer customer = (Customer) session.getAttribute(RedisConstants.USER_LOGIN_SESSION_KEY);
        if (customer == null) {
            return "redirect:http://localhost/auth/user/login.html";
        }
        List<Address> addresses = addressFeign.listByUid(customer.getId());
        Map<String, Object> map = carFeign.carListByGidAndUid(customer.getId(), gids);
        model.addAttribute("map", map);
        map.put("addresses", addresses);
        map.put("gids",gids);
        return page;
    }


    @RequestMapping("/add")
    @ResponseBody
    public String add(OrderParam orderParam, HttpSession session) {
        Customer customer = (Customer) session.getAttribute(RedisConstants.USER_LOGIN_SESSION_KEY);
        orderService.add(customer.getId(), orderParam);
        return "success";
    }

}
